#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
import os
import shutil
import xml.etree.ElementTree as etree
sys.path.append("E:/SVN/SGSDK/SGTools/SGAutoPack/SGPythonUtils")

gamedir = "E:/gamecopy"
fourdir = "E:/SVN/SGSDK/SGSMS/FOUR"
backupdir = "E:/SVN/SGSDK/SGTools/SGAutoPack/autopack_v4_ppl2/temp"

#检查资源相关的目录是否存在
def checkDirExist():
	if(False == os.path.exists(gamedir)):
		print( gamedir + " is not exist")
		raise( gamedir + " is not exist")
	if(False == os.path.exists(fourdir)):
		print( fourdir + " is not exist")
		raise( fourdir + " is not exist")
	if(False == os.path.exists(backupdir)):
		print( backupdir + " is not exist")
		raise( backupdir + " is not exist")

#copy文件
def copyFileSafety(backupfile,desfile):
	if( True == os.path.exists(backupfile) ):
		if( True == os.path.exists(desfile) ):
			os.remove(desfile)
		shutil.copyfile(backupfile,desfile)

#调整资源
def channelCustomOperBegin():
	print("begin , this is anzhi !")
	checkDirExist()

	#复制ps_logo_1.png文件到游戏目录中
	game_ps_logo_path = gamedir + "/assets/res/logo_01.png"
	backup_ps_logo_path = backupdir + "/anzhi/logo_01.png"
	copyFileSafety(backup_ps_logo_path,game_ps_logo_path)

	#替换图标
	anzhiIcon = backupdir + "/anzhi/icon_anzhi.png"
	gameIcon = gamedir + "/res/drawable/icon.png"
	gameIcon_hdpi = gamedir + "/res/drawable-hdpi/icon.png"
	gameIcon_ldpi = gamedir + "/res/drawable-ldpi/icon.png"
	gameIcon_mdpi = gamedir + "/res/drawable-mdpi/icon.png"
	gameIcon_xhpi = gamedir + "/res/drawable-xhdpi/icon.png"
	gameIcon_xxhpi = gamedir + "/res/drawable-xxhdpi/icon.png"
	copyFileSafety(anzhiIcon,gameIcon)
	copyFileSafety(anzhiIcon,gameIcon_hdpi)
	copyFileSafety(anzhiIcon,gameIcon_ldpi)
	copyFileSafety(anzhiIcon,gameIcon_mdpi)
	copyFileSafety(anzhiIcon,gameIcon_xhpi)
	copyFileSafety(anzhiIcon,gameIcon_xxhpi)

	#修改运营商
	projectfile = fourdir + "/project.properties"
	liblist = [
		'../../SGUtil/SGSDKUtils_Library',
		'../SmsLib/sms_lib_ayx_4.2.0',
		'../SmsLib/sms_lib_lt_4.0.2'
	]
	ConfigFileEdit.setProjectRefLib(projectfile,liblist)
	
	#替换payTypeConfig.xml文件
	gamePayTypeConfig = gamedir + "/assets/soulgame/payTypeConfig.xml"
	anzhiPayTypeConfig = backupdir + "/anzhi/anzhi_payTypeConfig.xml"
	copyFileSafety(anzhiPayTypeConfig,gamePayTypeConfig)

#恢复资源
def channelCustomOperEnd():
	print("end , this is anzhi !")
	checkDirExist()

	#还原运营商信息
	backupFourProjectFile = backuplib + "/four_project.properties"
	fourProjectFile = fourlib + "/project.properties"
	copyFileSafety(backupFourProjectFile,fourProjectFile)

	#因为是在copy工程中操作，ps_logo_1.png、图标、payTypeConfig.xml就不用还原了

if __name__=="__main__":
	if(sys.argv[1] == "begin"):
		channelCustomOperBegin()
	elif(sys.argv[1] == "end"):
		channelCustomOperEnd()
	else:
		raise("call channel custom operation must with param begin or end!")

#	channelCustomOperBegin()
#	channelCustomOperEnd()

